﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlTypes;
using Logic;
using Helper;
using DBAccess;
using WHUT;


public partial class tokenTeam_message : System.Web.UI.Page
{
    IPagedDataProvider pdp = PagedDataProviderFactory.Instance();

    private const string TITLE = "留言板";
    protected string introduce = string.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        pdp.ConnectionName = Common.GetDefaultSqlConnectionName();
        pdp.TableOrView = "t_messageboard";
        pdp.FieldsToReturn = "id,title, nickname, time_add, email, content";
        //pdp.FieldsToReturn = "*";
        pdp.FieldToOrder = "id";
        pdp.Ascending = false;
        pdp.PageSize = spPager.PageSize;
        pdp.RecordsFilter = "1=1";

        if (!IsPostBack)
        {
            BindMessage(true);
            BindIntr();
        }
    }

    private void BindMessage(bool bResetPager)
    {
        if (bResetPager)
        {
            spPager.CurrentPageIndex = 1;
            spPager.RecordsCount = pdp.TotalRecords;
        }

        pdp.CurrentPageIndex = spPager.CurrentPageIndex;
        rptMsgBox.DataSource = pdp.GetCurrentPageAsDataTable().DefaultView;
        rptMsgBox.DataBind();
    }

    /// <summary>
    /// 翻页控制
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SimplePager1_PageIndexChanging(object sender, wbh.PageChangingEvent e)
    {
        spPager.CurrentPageIndex = e.NewIndex;
        BindMessage(false);
    }


    protected void repMsgBox_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        string selStr = string.Empty;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rep = e.Item.FindControl("rpt_MsgRelpy") as Repeater;//找到里层的repeater对象
            DataRowView rowv = (DataRowView)e.Item.DataItem; //找到分类Repeater关联的数据项 
            String deptId = Convert.ToString(rowv["Id"]);
            //String cateId = ((PetShop.Model.CategoryInfo)e.Item.DataItem).Id;

            selStr = "SELECT [content],[reply_time] FROM [t_message_reply] WHERE [msg_id]=" + deptId;
            DataTable dt = Common.SQLQueryAsDataTable(Common.GetDefaultDBConnection(), selStr);
            rep.DataSource = dt;
            rep.DataBind();
        }
    }

    protected void ibSubmit_Click(object sender, EventArgs e)
    {
        string validateNum = tbValidateNum.Text;
        // valid code ?
        if (Conf.CreateFromCurrentSession().get("tokenmessage") == null || Conf.CreateFromCurrentSession().get("tokenmessage").ToString() != tbValidateNum.Text)
        {
            lbValidateErr.Text = "验证码错误。";
            lbValidateErr.Visible = true;
            tbValidateNum.Focus();
            return;
        }

        SqlString nickName = String.IsNullOrEmpty(tbNickName.Text.Trim()) ? "匿名网友" : tbNickName.Text.Trim();
        SqlString title = String.IsNullOrEmpty(tbTitle.Text.Trim()) ? string.Empty : tbTitle.Text.Trim();
        SqlString email = String.IsNullOrEmpty(tbMail.Text.Trim()) ? string.Empty : tbMail.Text.Trim();
        SqlString content = tbContent.Text.Trim();
        

        try
        {
            //Common.SQLUpdate(Common.GetDefaultDBConnection(), "INSERT INTO [dbo].[t_message] ([title],[content],[file_url],[sender],[receiver],[is_read],[sender_delete],[receiver_delete],[time_post])  VALUES ( @1,@2,@3,@4,@5,@6,@7,@8,@9 );", true, new object[] { _title, _content, _file_url, _sender, _receiver, _is_read, _sender_delete, _receiver_delete, _time_post });
            Common.SQLUpdate(Common.GetDefaultDBConnection(), "INSERT INTO [dbo].[t_messageboard] ([title],[nickname],[time_add],[email],[content])  VALUES ( @1,@2,@3,@4,@5);", true, new object[] { title, nickName , (SqlDateTime)DateTime.Now, email , content });
        }
        catch (Exception ex)
        {
            Response.Redirect("~/error.htm");
            return;
        }

        BindMessage(true);
    }

    private void BindIntr()
    {
        Introduce into = (new Introduce()).LoadByTags(TITLE);
        introduce = into.Content;
    }
}
